package myhibernate;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * TAdmin entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see myhibernate.TAdmin
 * @author MyEclipse Persistence Tools
 */

public class TAdminDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(TAdminDAO.class);
	// property constants
	public static final String ADMIN_USERNAME = "adminUsername";
	public static final String ADMIN_PASSWORD = "adminPassword";

	public boolean login(TAdmin instance) {
		log.debug("finding TAdmin instance by example");
		Session session = getSession();
		try {
			List results = session.createCriteria("myhibernate.TAdmin").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return !results.isEmpty();
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			re.printStackTrace();
			throw re;
		} finally {
			session.close();
		}
	}

	public void save(TAdmin transientInstance) {
		log.debug("saving TAdmin instance");
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			if (tx != null) {
				tx.rollback();
			}
			log.error("save failed", re);
			re.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void delete(TAdmin transientInstance) {
		log.debug("deleting TAdmin instance");
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(transientInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			if (tx != null) {
				tx.rollback();
			}
			log.error("delete failed", re);
			re.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void update(TAdmin transientInstance) {
		log.debug("updating TAdmin instance");
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			getSession().update(transientInstance);
			tx.commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			if (tx != null) {
				tx.rollback();
			}
			log.error("update failed", re);
			re.printStackTrace();
		} finally {
			session.close();
		}
	}

	public TAdmin findById(java.lang.Integer id) {
		log.debug("getting TAdmin instance with id: " + id);
		Session session = getSession();
		try {
			TAdmin instance = (TAdmin) session.get("myhibernate.TAdmin", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	public List findByExample(TAdmin instance) {
		log.debug("finding TAdmin instance by example");
		try {
			List results = getSession().createCriteria("myhibernate.TAdmin")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TAdmin instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TAdmin as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAdminUsername(Object adminUsername) {
		return findByProperty(ADMIN_USERNAME, adminUsername);
	}

	public List findByAdminPassword(Object adminPassword) {
		return findByProperty(ADMIN_PASSWORD, adminPassword);
	}

	public List findAll() {
		log.debug("finding all TAdmin instances");
		Session session = getSession();
		try {
			String queryString = "from TAdmin";
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	public TAdmin merge(TAdmin detachedInstance) {
		log.debug("merging TAdmin instance");
		try {
			TAdmin result = (TAdmin) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TAdmin instance) {
		log.debug("attaching dirty TAdmin instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TAdmin instance) {
		log.debug("attaching clean TAdmin instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}